#include "10_18.h";
#include<iostream>
using namespace std;
void SeqListInit(SL* ps)
{
	
	ps->size = 0;
	ps->capacity = 0;
	ps->a = NULL;
}
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		cout << ps->a[i]<<" ";
	}
}

void SeqListPushBack(SL* ps, SLDataType x)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->a = tmp;
	}
	ps->a[ps->size] = x;
	ps->size++;
}
void SeqListPopBack(SL* ps)
{
	if (ps->size > 0)
	{
		ps->size--;
	}
}
void SeqListPushFront(SL* ps, SLDataType x)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
		
	}
	int end = ps->size - 1;
	while (end>= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
void SeqListPopFront(SL* ps)
{
	if (ps->size <= 0)
	{
		exit(-1);
	}
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

int SeqListFind(SL* ps, SLDataType x) {
	for (int i = 0; i < ps->size; i++) {
		if (ps->a[i] == x)
		{
			return i;
		}


	}
	return -1;
}
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
	if (pos > ps->size || pos < 0)
	{
		exit(-1);
	}
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;

}

void SeqListErace(SL* ps, int pos)
{
	if (pos<0 || pos>ps->size)
	{
		exit(-1);
	}
	while (pos < ps->size-1)
	{
		ps->a[pos] = ps->a[pos + 1];
		pos++;
	}
	pos--;
	ps->size--;
}